package com.firmboy.big;

import com.firmboy.staticdb.Count;
import com.firmboy.staticdb.DBPoolHelper;
import com.firmboy.threadPool.PoolFactory;
import org.junit.Test;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author firmboy
 * @create 2018-04-21 下午5:15
 **/
public class CreateBigData {

    @Test
    public  void te(){

    }

    @Test
    public void createBigDataWithThread(){
        long start = System.currentTimeMillis();
        final int page = 10000;
        final CountDownLatch countDownLatch = new CountDownLatch(10);
        final Count count = new Count();

        ThreadPoolExecutor pool = PoolFactory.getPool();

        System.out.println("count初始值："+count.getCount());

        Runnable my = new Runnable() {
            @Override
            public void run() {

                while(count.getCountSyn()<page){
                    System.out.println("当前的count值："+count.getCount());
                    ArrayList<List<Object>> lists = new ArrayList<>();
                    long l1 = System.currentTimeMillis();
                    lists = new ArrayList<>();
                    for (int i=0;i<100;i++){
                        lists.add(getObj());
                    }
                    DBPoolHelper.insertBatch("insert into test_user values",lists);
                    long l2 = System.currentTimeMillis();
                    //System.out.println(100+"条数据插入耗时："+(l2-l1)+"毫秒");

                }
                countDownLatch.countDown();
            }
        };

        for (int i = 0;i<10;i++){
            pool.execute(my);
        }

        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("整体结束，耗时："+(end-start)+"毫秒");

    }


    @Test
    public void createBigData(){
        int page = 100;
        long start = System.currentTimeMillis();



        ArrayList<List<Object>> lists = new ArrayList<>();
        for (int j= 0;j<page;j++){
            long l1 = System.currentTimeMillis();
            lists = new ArrayList<>();
            for (int i=0;i<100;i++){
                lists.add(getObj());
            }
            DBPoolHelper.insertBatch("insert into test_user values",lists);
            long l2 = System.currentTimeMillis();
            System.out.println(100+"条数据插入耗时："+(l2-l1)+"毫秒");
            System.out.println("当前j："+j);
        }

        long end = System.currentTimeMillis();
        System.out.println(page*100+"条数据插入耗时："+(end-start)+"毫秒");


    }

    public List<Object> getObj(){
        ArrayList<Object> obj = new ArrayList<>();
        obj.add(createid());
        obj.add(createUserName());
        obj.add(createPassWrod());
        obj.add(createName());
        obj.add(createSex());
        obj.add(createAge());
        obj.add(createCardId());
        obj.add(createHigh());
        obj.add(createWeight());
        obj.add(createQQ());
        obj.add(createWeixin());
        return obj;

    }

    public int getRandom(int end){
        Random random = new Random();
        return random.nextInt(end);
    }

    public int getRandom(int start,int end){
        int i = 0;
        if(start<=end){
            Random random = new Random();
            while(true){
                i = random.nextInt(end+1);
                if(i>=start){
                    break;
                }

            }
        }
        return i;
    }

    /**
     * 获取一个固定长度的字符串，如果不够就咋前面加0补充
     * @param str
     * @param length
     * @return
     */
    public String getLengthString(String str,int length){
        if(str.length()>=length){
            return str;
        }else{
            StringBuffer sb = new StringBuffer();
            for (int i = str.length();i<length;i++){
                sb.append("0");
            }
            sb.append(str);
            return sb.toString();
        }
    }



    /**
     * 随机生成id
     * @return
     */
    public String createid(){
        return UUID.randomUUID().toString().replace("-","");
    }

    /**
     * 随机生成用户名
     * @return
     */
    public String createUserName(){
        char[] chars = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
        int random = getRandom(5, 10);
        StringBuffer sb = new StringBuffer();
        for(int i=0;i<random;i++){
            sb.append(chars[getRandom(chars.length-1)]);
        }
        return sb.toString();
    }

    /**
     * 随机生成密码
     * @return
     */
    public String createPassWrod(){
        char[] chars = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
        int random = getRandom(6, 12);
        StringBuffer sb = new StringBuffer();
        for(int i=0;i<random;i++){
            sb.append(chars[getRandom(chars.length-1)]);
        }
        return sb.toString();
    }

    /**
     * 随机生成name
     * @return
     */
    public String createName(){
        String[] fname = {"赵","钱","孙","李","周","武","杨","刘","徐","肖","诸葛","欧阳","司马","王","郭","吴","黎","范","于","龙","顾"};
        String[] mname = {"一","二","三","四","五","六","七","八","九","十"};
        StringBuffer sb = new StringBuffer();
        sb.append(fname[getRandom(fname.length - 1)]);

        int random = getRandom(1, 2);
        for (int i = 0;i<random;i++){
            sb.append(mname[getRandom(mname.length-1)]);
        }
        return sb.toString();
    }

    /**
     * 随机生成性别
     * @return
     */
    public String createSex(){
        return getRandom(0,1)+"";
    }

    /**
     * 随机生成年龄
     * @return
     */
    public int createAge(){
        return getRandom(1,100);
    }

    /**
     * 随机生成省份证号
     * @return
     */
    public String createCardId(){
        //getRandom()
        StringBuffer sb = new StringBuffer();
        //省
        sb.append(getLengthString(getRandom(1,32)+"",2));
        //市
        sb.append(getLengthString(getRandom(1,17)+"",2));
        //县
        sb.append(getLengthString(getRandom(1,8)+"",2));
        //年
        sb.append(getLengthString(getRandom(1900,2008)+"",4));
        //月
        sb.append(getLengthString(getRandom(1,12)+"",2));
        //日
        sb.append(getLengthString(getRandom(1,30)+"",2));
        //随机号
        sb.append(getLengthString(getRandom(1,9999)+"",4));

        return sb.toString();
    }

    /**
     * 随机生成身高
     * @return
     */
    public String createHigh(){
        StringBuffer sb = new StringBuffer();
        sb.append(getRandom(160,190)+"");
        sb.append(".");
        sb.append(getLengthString(getRandom(0,99)+"",2));
        return sb.toString();
    }

    /**
     * 随机生成体重
     * @return
     */
    public String createWeight(){
        StringBuffer sb = new StringBuffer();
        sb.append(getRandom(45,90)+"");
        sb.append(".");
        sb.append(getLengthString(getRandom(0,99)+"",2));
        return sb.toString();
    }

    /**
     * 随机生成qq号
     * @return
     */
    public String createQQ(){
        int random = getRandom(8, 9);

        StringBuffer sb = new StringBuffer();
        sb.append(getRandom(1,9));
        for (int i=0;i<random;i++){
            sb.append(getRandom(0,9));
        }
        return sb.toString();
    }

    /**
     * 随机生成微信号
     * @return
     */
    public String createWeixin(){

        return UUID.randomUUID().toString().replace("-","").substring(0,24);
    }
}